<?php
	/*****************************************************************
		GP Clinic - a web based Medical directory system
		Copyright (C) 2014 Kaizensynergy - Gemba360

		Author: Bless Bauson
		Key: bHkTy89wZ0DHyUiim0041rTxyBbP671aQwg327UtjfcrO
	*****************************************************************/
	
	$add 	= fetchAddFormParameters();
	$search = fetchSearchFormParameters();

	//search form filter submit
	$obj = new Patient();
	if($_SERVER['REQUEST_METHOD'] == "POST"){

		if(isset($_POST['inactive']) || isset($_POST['deceased'])){
			$obj->filter = "( patient.active = 1";
			if($_POST['inactive'] == 1){
				$obj->filter .= " OR patient.active = 0 ";
			}
			if($_POST['deceased'] == 1){
				$obj->filter .= " OR patient.active = 2 ";
			}
			$obj->filter .= " ) ";
		} else {
			$obj->filter = "patient.active = 1";
		}

		if(isset($_POST['searchby']) && !empty($_POST['searchby']) && !empty($_POST['keyword'])){
			$obj->SQL_AddFilter("patient.".$_POST['searchby']." LIKE '%".$_POST['keyword']."%'");
		}
	}

	//if not super administrator, filter results to all patients of this user
	if($_SESSION['account']['user_type_id'] != CONST_SUPER_ADMIN_TYPE){
		$obj->SQL_AddJoin("LEFT JOIN", "user_patient AS up", "patient.id = up.patient_id", "up.user_id");

		if(!empty($_SESSION['account']['user_id'])){
			$obj->SQL_AddFilter("up.user_id = ".$_SESSION['account']['user_id']);
		}
	}

	//filter result list based on status
	if($_SERVER['REQUEST_METHOD'] == "GET" && isset($_GET['status'])){
		$obj->SQL_AddFilter("patient.active = ".$_GET['status']);
	}

	//set order of results
	if($_SERVER['REQUEST_METHOD'] == "GET" && isset($_GET['o'])){
		$obj->order = "patient.".$_GET['o']." ASC";
	}

	//view all using pagination
	$sql 		= $obj->SQL_FormQuery();
	$cntrows	= $obj->SQL_Count();

	//form pagination
	$rowcntPerPage  = CONST_PAGE_ROW_COUNT;
	$patientObj = new Pagination(
				$sql,				//query resultset
				$rowcntPerPage,		//max # of rows displayed
				'page',				//pagination variable used in GET or POST
				5					//number of links displayed in getPageNav
			);
 ?>